/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * formSewa.java
 *
 * Created on Mar 30, 2012, 10:02:35 AM
 */
package aplikasi.com.gui;

import aplikasi.com.entity.catatSewa;
import aplikasi.com.entity.komik;
import aplikasi.com.entity.pelanggan;
import aplikasi.com.implement.implemCatatSewa;
import aplikasi.com.implement.implemKomik;
import aplikasi.com.implement.implemPelanggan;
import aplikasi.com.interfac.interCatatSewa;
import aplikasi.com.interfac.interKomik;
import aplikasi.com.interfac.interPelanggan;
import aplikasi.com.koneksiDatabase.DatabaseUtilities;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Henfry
 */
public class formSewa extends javax.swing.JInternalFrame {

    public int idSewa = 0;
    private interKomik dataKomik;
    private interPelanggan dataPelanggan;
    private interCatatSewa dataSewa;
    public List<komik> dtKomik = new ArrayList<komik>();
    public List<pelanggan> dtPelanggan = new ArrayList<pelanggan>();
    Calendar TimeTest = Calendar.getInstance();
    Date waktuSekarang = TimeTest.getTime();
    SimpleDateFormat tanggal = new SimpleDateFormat("dd-MM-yyyy");
    
    /** Creates new form formSewa */
    public formSewa() throws SQLException {
        initComponents();
        dataKomik = new implemKomik();
        dataPelanggan = new implemPelanggan();
        dataSewa = new implemCatatSewa();
        this.statusAwal();
    }

    public void statusAwal() throws SQLException {
        this.LoadJudulKomik();
        this.LoadDataPelanggan();
        txtNoSewa.setText(String.valueOf(cariIDSewa()));
        LabelTanggalSewa.setText(tanggal.format(waktuSekarang));
    }

    public void LoadJudulKomik() throws SQLException {
        comboJudulKomik.removeAllItems();
        dtKomik = dataKomik.viewKomik();
        for (komik km : dtKomik) {
            comboJudulKomik.addItem(km.getJudulKomik());
        }

    }

    public void LoadDataPelanggan() throws SQLException {
        comboidPelanggan.removeAllItems();
        dtPelanggan = dataPelanggan.viewPelanggan();
        for (pelanggan pel : dtPelanggan) {
            comboidPelanggan.addItem(pel.getIdPelanggan());
        }

    }

    int cariIDSewa() {
        try {
            String sql = "SELECT idSewa FROM datasewa ORDER BY idSewa DESC;";
            Statement st = DatabaseUtilities.getConnection().createStatement();
            ResultSet rs = st.executeQuery(sql);
            if (rs.next()) {
                idSewa = rs.getInt("idSewa");
            }
        } catch (SQLException ex) {

            Logger.getLogger(formTambahPelanggan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ++idSewa;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        comboidPelanggan = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        comboJudulKomik = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtnamaPenyewa = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtNoSewa = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        comboLamaSewa = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        ButtonProses = new javax.swing.JButton();
        btnBatal = new javax.swing.JButton();
        btnKeluar = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        LabelTanggalSewa = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtTanggalKembali = new javax.swing.JTextField();
        txtHargaSewa = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setResizable(true);
        setTitle("Form Sewa Komik");

        jLabel1.setText("ID Pemakai ");

        comboidPelanggan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel2.setText("Judul Komik");

        comboJudulKomik.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboJudulKomik.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboJudulKomikActionPerformed(evt);
            }
        });

        jLabel3.setText("Nama");

        jLabel4.setText("Harga Sewa");

        jLabel7.setText("No Sewa");

        jLabel5.setText("Lama Sewa");

        comboLamaSewa.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5" }));
        comboLamaSewa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboLamaSewaActionPerformed(evt);
            }
        });

        jLabel6.setText("Hari");

        ButtonProses.setText("Proses");
        ButtonProses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonProsesActionPerformed(evt);
            }
        });

        btnBatal.setText("Batal");

        btnKeluar.setText("Keluar");
        btnKeluar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKeluarActionPerformed(evt);
            }
        });

        jLabel8.setText("Tanggal :");

        LabelTanggalSewa.setText("Tanggal");

        jLabel10.setText("Tanggal Kembali");

        txtTanggalKembali.setText("Tanggal Kembali");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel1)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboidPelanggan, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(comboJudulKomik, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtnamaPenyewa, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(comboLamaSewa, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtNoSewa, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(182, 182, 182)
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(LabelTanggalSewa))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel6))))
                            .addComponent(txtTanggalKembali, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtHargaSewa, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ButtonProses)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBatal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnKeluar)))
                .addContainerGap(99, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {ButtonProses, btnBatal, btnKeluar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(LabelTanggalSewa)
                    .addComponent(txtNoSewa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(comboidPelanggan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(comboJudulKomik, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtnamaPenyewa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtHargaSewa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(comboLamaSewa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(txtTanggalKembali, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ButtonProses)
                    .addComponent(btnBatal)
                    .addComponent(btnKeluar))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnKeluarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKeluarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnKeluarActionPerformed

    private void ButtonProsesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonProsesActionPerformed
        // TODO add your handling code here:
        try {
            catatSewa c = new catatSewa();
            pelanggan p = new pelanggan(Integer.parseInt(comboidPelanggan.getSelectedItem().toString()));
            komik k = new komik(dataKomik.getIdKomik(comboJudulKomik.getSelectedItem().toString()));
            c.setIdSewa(Integer.parseInt(txtNoSewa.getText()));
//            c.setTanggalSewa(Date.parse(LabelTanggalSewa.getText()));
//            c.setTanggalKembali(Date.parse(txtTanggalKembali.getText()));
            c.setDataid(k);
            c.setDataidP(p);
            dataSewa.insertSewa(c);
        } catch (SQLException ex) {
            Logger.getLogger(formSewa.class.getName()).log(Level.SEVERE, null, ex);
        }



    }//GEN-LAST:event_ButtonProsesActionPerformed

    private void comboLamaSewaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboLamaSewaActionPerformed
        // TODO add your handling code here:
        int i = Integer.parseInt(comboLamaSewa.getSelectedItem().toString());
        TimeTest.add(Calendar.DATE, i);
        Date waktuAtur = TimeTest.getTime();
        String TanggalFormat = tanggal.format(waktuAtur);
        txtTanggalKembali.setText(TanggalFormat);
    }//GEN-LAST:event_comboLamaSewaActionPerformed

    private void comboJudulKomikActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboJudulKomikActionPerformed
//        try {
//            txtHargaSewa.setText(String.valueOf(dataKomik.gettarifSewa(comboJudulKomik.getSelectedItem().toString())));
//        } catch (SQLException ex) {
//            System.out.println("Error " + ex);
//        }
    }//GEN-LAST:event_comboJudulKomikActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonProses;
    private javax.swing.JLabel LabelTanggalSewa;
    private javax.swing.JButton btnBatal;
    private javax.swing.JButton btnKeluar;
    private javax.swing.JComboBox comboJudulKomik;
    private javax.swing.JComboBox comboLamaSewa;
    private javax.swing.JComboBox comboidPelanggan;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField txtHargaSewa;
    private javax.swing.JTextField txtNoSewa;
    private javax.swing.JTextField txtTanggalKembali;
    private javax.swing.JTextField txtnamaPenyewa;
    // End of variables declaration//GEN-END:variables
}
